package com.wsd.bookkeeping.controller;


import com.wsd.bookkeeping.entity.Comment;
import com.wsd.bookkeeping.entity.query.CommentQuery;
import com.wsd.bookkeeping.service.CommentService;
import com.wsd.common.entity.ResponseEntity;
import com.wsd.common.utils.DateUtils;
import com.wsd.common.utils.ResponseUtils;
import org.apache.coyote.Response;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 评论相关
 */
@RestController
@RequestMapping("/comment")
public class CommentController {

    @Resource
    private CommentService commentService;

    /**
     * 保存评论内容
     * @param comment 评论对象
     * @param request 请求对象
     * @return response
     */
    @PostMapping("/save")
    public ResponseEntity comment(@RequestBody Comment comment, HttpServletRequest request) {
        comment.setCreateDate(DateUtils.currentDateNString());
        comment.setUpdateDate(DateUtils.currentDateNString());
        comment.setCreateIp(request.getRemoteHost());
        return ResponseUtils.ret(commentService.save(comment));
    }

    @GetMapping("/{bookId}/pn/{curPage}")
    public ResponseEntity queryByPage(CommentQuery commentQuery) {
        return ResponseEntity.success().put("page", commentService.queryByPage(commentQuery));
    }

    /**
     * 查询账本展示信息
     * 账本创建时间 && 账本用户列表 && 账本评论数量
     * @param bookId
     * @return
     */
    @GetMapping("/query/info/{bookId}")
    public ResponseEntity queryInfo(@PathVariable("bookId") Integer bookId) throws InterruptedException {
        return ResponseEntity.success().put("info", commentService.queryBookInfo(bookId));
    }


}
